package com.study.concurrent.atomic;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Name: AtomicStampedReferenceTest
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/4/14 14:16
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class AtomicStampedReferenceTest {
    public AtomicStampedReference<Integer> atomicStampedReference =new AtomicStampedReference<Integer>(1,0);
    public boolean increment() {
       return atomicStampedReference.compareAndSet(atomicStampedReference.getReference(),
                atomicStampedReference.getReference()+1,
                atomicStampedReference.getStamp(),
                atomicStampedReference.getStamp()+1);
    }
    // 使用 AtomicInteger 后，不需要加锁，也可以实现线程安全
    public Integer getCount() {
        return atomicStampedReference.getReference();
    }
    public int getStamp() {
        return atomicStampedReference.getStamp();
    }
}
